package com.chanceit.ssss.popuwindow;

import org.gl.GlobalVariable;
import org.gl.utils.AndroidUtil;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.chanceit.ssss.activity.R;
import com.chanceit.ssss.button.CallPhoneListener;

public class ButtonsPopuWindow {
	PopupWindow popup;
	Context context;
	int heigh = AndroidUtil.dipToPx(250);
	int weith = AndroidUtil.dipToPx(128);

	public ButtonsPopuWindow(Context c) {
		this.context = c;
	};

	View.OnClickListener mOnClickListener;

	public void setOnClick(View.OnClickListener mOnClickListener) {
		this.mOnClickListener = mOnClickListener;
	}

	@SuppressWarnings("deprecation")
	public void showWindow(View parent) {
		if (popup == null) {
			LayoutInflater layoutInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View view = layoutInflater.inflate(R.layout.popuwindows_menu, null);
			if (mOnClickListener != null) {
				view.findViewById(R.id.button1).setOnClickListener(
						mOnClickListener);
				view.findViewById(R.id.button2).setOnClickListener(
						mOnClickListener);
				view.findViewById(R.id.button3).setOnClickListener(
						mOnClickListener);
				view.findViewById(R.id.button4).setOnClickListener(
						mOnClickListener);
				view.findViewById(R.id.button5).setOnClickListener(
						mOnClickListener);
			}
			// 创建一个PopuWidow对象
			popup = new PopupWindow(view, weith, heigh);
			// 设置焦点在弹窗上
			popup.setFocusable(true);
			// 设置允许在外点击消失
			popup.setOutsideTouchable(true);
			// 设置弹窗消失事件监听
			popup.setOnDismissListener(new OnDismissListener() {
				public void onDismiss() {
					// TODO Auto-generated method stub
					// image.setImageResource(R.drawable.toolbar_plus);
				}
			});
			// 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
			// new
			// AndroidDrawableUtil().drawLayoutDropShadow(view,AndroidUtil.dipToPx(135),AndroidUtil.dipToPx(298));
			popup.setBackgroundDrawable(new ColorDrawable(0x00000000));
			popup.setTouchInterceptor(new OnTouchListener() {
				public boolean onTouch(View view, MotionEvent event) {
					if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
						popup.dismiss();
						// image.setImageResource(R.drawable.toolbar_plus);
						return true;
					}
					return false;
				}
			});
		}
		if (!popup.isShowing()) {
			popup.showAsDropDown(parent,
					GlobalVariable.disPlay_w - AndroidUtil.dipToPx(142), 0);
		}
	}

	public void showWindow(View parent, JSONObject selectCarerInfo) {
		View view = getView(R.layout.popuwindows_menu, selectCarerInfo);
		// 创建一个PopuWidow对象
		popup = new PopupWindow(view, weith, heigh);
		// 设置焦点在弹窗上
		popup.setFocusable(true);
		// 设置允许在外点击消失
		popup.setOutsideTouchable(true);
		// 设置弹窗消失事件监听
		popup.setOnDismissListener(new OnDismissListener() {
			public void onDismiss() {
				// image.setImageResource(R.drawable.toolbar_plus);
			}
		});
		// 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
		// new
		// AndroidDrawableUtil().drawLayoutDropShadow(view,AndroidUtil.dipToPx(135),AndroidUtil.dipToPx(298));
		popup.setBackgroundDrawable(new ColorDrawable(0x00000000));
		popup.setTouchInterceptor(new OnTouchListener() {
			public boolean onTouch(View view, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
					popup.dismiss();
					// image.setImageResource(R.drawable.toolbar_plus);
					return true;
				}
				return false;
			}
		});
		if (!popup.isShowing()) {
			popup.showAsDropDown(parent,
					GlobalVariable.disPlay_w - AndroidUtil.dipToPx(142), 0);
		}

	}

	public View getView(int resoucId, JSONObject selectCarerInfo) {
		LayoutInflater layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = layoutInflater.inflate(resoucId, null);
		setOnclick(selectCarerInfo, view);
		TextView tv_count_call = ((TextView) view.findViewById(R.id.textView1));
		int c1 = selectCarerInfo.optInt("phoneCount");
		if (c1 == 0) {
			tv_count_call.setVisibility(View.GONE);
		} else {
			tv_count_call.setText("" + c1);
		}
		TextView tv_count_msg = ((TextView) view.findViewById(R.id.textView2));
		int c2 = selectCarerInfo.optInt("messageCount");
		if (c2 == 0) {
			tv_count_msg.setVisibility(View.GONE);
		} else {
			tv_count_msg.setText("" + c1);
			if (c1>=3) {
				tv_count_msg.setBackgroundResource(R.drawable.circle_red);
			}
		}
		return view;
	}

	public void setOnclick(JSONObject selectCarerInfo, View view) {
		if (mOnClickListener != null) {
			view.findViewById(R.id.button1).setTag(selectCarerInfo);
			view.findViewById(R.id.button1).setOnClickListener(
					new CallPhoneListener(selectCarerInfo));
			view.findViewById(R.id.button2)
					.setOnClickListener(mOnClickListener);
			view.findViewById(R.id.button3)
					.setOnClickListener(mOnClickListener);
			view.findViewById(R.id.button4)
					.setOnClickListener(mOnClickListener);
			view.findViewById(R.id.button5)
					.setOnClickListener(mOnClickListener);
		}
	}
	public void dismiss(){
		if (popup!=null) {
			popup.dismiss();
		}
	}
}
